<?php
class faultModel extends RelationModel{
    
    
    public function _initialize()
    {
        parent::_initialize();
        $this->faultType =  $_REQUEST['faultType']?trim($_REQUEST['faultType']):'modelFault';      
    }    
    
    protected $_auto = array ( 
        array('addTime','time','1','function'),    
        array('addUserId','getAdminId',1,'function') , // 对password字段在新增的时候使md5函数处理
        array('step',1),
        array('create_time','time',2,'function'), // 对create_time字段在更新的时候写入当前时间戳
    
    );

    //发起关联，给后台使用，前台API 不需要使用这个
    protected $_link = array(
        'user' => array(
            'mapping_type' => BELONGS_TO,
            'class_name' => 'admin',
            'foreign_key' => 'addUserId',
            'as_fields'=>'userName,realName',
        ),
        'store' => array(
            'mapping_type' => BELONGS_TO,
            'class_name' => 'store',
            'foreign_key' => 'storeId',
            'as_fields'=>'regionId,bcId,name,channel',
        ), 
        'region' => array(
            'mapping_type' => BELONGS_TO,
            'class_name' => 'region',
            'foreign_key' => 'regionId',
            'as_fields'=>'name:regionName',
        ),  
    ); 
    
    /**
     * 取一条或者多条数据
     */ 
    public function getDetail($ids){
        $ids = trim($ids,',');
        $ids .=',';
        $list = $this->relation(true)->where(array('id'=>array('in',$ids)))->select();
        $pids = '';
        foreach($list as $k=>&$v){
            $pids .=$v['id'].',';
            $v['photo'] = unserialize($v['photo']);
        }
        $value = D('attribute')->getAttributeValue($this->faultType,$pids);
        foreach($list as $k=>&$v){
            $v['value'] = $value[$v['id']];
        }
        return $list;
    }
    
    
    
    /**
     * 插入后，在属性表记录数据
     */ 
     protected function _after_insert($data,$option){     
        $this->faultType=$_REQUEST['faultType']?trim($_REQUEST['faultType']):'modelFault';
        $pid = $data['id'];
        $valArr = array();
        foreach($_POST['choices'] as $k=>$v){
            if(is_array($v)){
                $v = serialize($v);
            }
            $temp['pid'] = $pid;
            $temp['modular'] = $this->faultType;
            $temp['value']  = $v;
            $temp['optionid'] = $k;
            $valArr[] = $temp;
        }
        if(!empty($valArr)){
            M('attributevar')->addAll($valArr);
        }        
     }
     
    
    protected function _before_update($data,$option){
        if(empty($_POST['regionId'])){
            unset($data['regionId']);
            unset($data['storeId']);
        }
        if(empty($_POST['brandId'])){
            unset($data['brandId']);
        }
        if(empty($_POST['modelId'])){
            unset($data['modelId']);
        }        
        return $data;
    }
    
     /**
     * 更新后，在属性表记录数据
     */     
     protected function _after_update($data,$option){   
        $this->faultType=$_REQUEST['faultType']?trim($_REQUEST['faultType']):'modelFault';
        $pid = $data['id'];        
        $valArr = array();
        foreach($_POST['choices'] as $k=>$v){
            if(is_array($v)){
                $v = serialize($v);
            }
            $temp['pid'] = $pid;
            $temp['modular'] = $this->faultType;
            $temp['value']  = $v;
            $temp['optionid'] = $k;
            $valArr[] = $temp;
        }
        if(!empty($valArr)){
            M('attributevar')->where(array('pid'=>$pid,'modular'=>$this->faultType))->delete();
            M('attributevar')->addAll($valArr);
        }        
    }
	
	/**
	 * 获得体验机品牌列表
	 * @return array
	 */
	public function getModelBrand() {
		$data = array();
		$res = D('model')->alias('m')->field(array('b.id'=>'brandId','b.name'=>'brandName'))
				->join(C('DB_PREFIX')."brand AS b ON b.id = m.brandId")
				->where(array('m.isTest'=>1, 'm.status'=>1))->select();
		foreach ($res as $row){
			$data[$row['brandId']] = array('brandId'=>$row['brandId'], 'brandName'=>$row['brandName']);
		}
		sort($data);
		return $data;
	}
	
	
	/**
	 * 获得展示柜类型
	 * @return array
	 */
	public function getShowcase(){
		$showcaseFault = D('attribute')->getAttribute('showcaseFault');
		echo json_encode($showcaseFault);
		foreach($showcaseFault as $row){
			if($row['modular'] == 'showcase'){
				return $row['rules']['choices'];
			}
		}
	}
	
} 
